<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Customer.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Customer_Models_Dao_Pdo_Mysql_Customer extends Tomato_Model_Dao
	implements Customer_Models_Interface_Customer
{
	public function convert($entity) 
	{
		return new Customer_Models_Customer($entity); 
	}
	// Tìm mã số tiếp theo của sản phẩm thêm mới vào
	public function getNextId(){		
		$config 					= Tomato_Config::getConfig(); 
		$prefix_code_customer		= $config->get('prefix_code')->customer_code;
		$prefix_code_customer_length = strlen($prefix_code_customer);
		$name_ini_length			= 'customer_code_length';
		$length_max_id				= $config->get('prefix_code')->$name_ini_length;
			
		$select = $this->_conn
					->select()
					->from(array('p' => $this->_prefix . 'customer'),'MAX(SUBSTR(p.id,'.($prefix_code_customer_length+1).')) as MAX_ID');
		$row = $select->query()
			   		  ->fetch();	
		$max_id =  (null == $row) ? 0 : $row->MAX_ID;
		$max_id++;
		$addition_zero_num 		= $length_max_id - strlen($max_id) - strlen($prefix_code_customer);
		
		$code = $prefix_code_customer;
		for($i=1;$i<=$addition_zero_num;$i++)
			$code.='0';
		$code.= $max_id;
		return $code;	
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'customer'));
		if ($exp) {
            if (isset($exp['customer_id'])) {
				$select->where("c.id=?",$exp['customer_id']);
			}
            if (isset($exp['name'])) {
				$select->where("c.name= LIKE '%".$exp['name']."%'");				
			}
            if (isset($exp['tel'])) {
				$select->where("c.tel= LIKE '%".$exp['tel']."%'");				
			}
            if (isset($exp['address'])) {
				$select->where("c.address= LIKE '%".$exp['address']."%'");				
			}
            if (isset($exp['birthday'])) {
				$select->where("c.birthday= ?",$exp['birthday']);				
			}
            if (isset($exp['sex'])) {
				$select->where("c.sex= ?",$exp['sex']);				
			}
            if (isset($exp['first_time'])) {
				$select->where("c.first_time >= ",$exp['first_time']);				
			}
            if (isset($exp['last_time'])) {
				$select->where("c.last_time <= ?",$exp['last_time']);				
			}
            if (isset($exp['amount_time'])) {
				$select->where("c.amount_time >= ?",$exp['amount_time']);				
			}
            if (isset($exp['first_deposit'])) {
				$select->where("c.first_deposit >= ?",$exp['first_deposit']);				
			}
            if (isset($exp['remaining_money'])) {
				$select->where("c.remaining_money >= ?",$exp['remaining_money']);				
			}	
            if (isset($exp['employee_id'])) {
				$select->where("c.employee_id = ?",$exp['employee_id']);				
			}
			if(isset($exp['referrer_id'])){
				$select->where("c.referrer_id = ?",$exp['referrer_id']);
			}
			if(isset($exp['email'])){
				$select->where("c.email = ?",$exp['email']);
			}
			if(isset($exp['used_money'])){
				$select->where("c.used_money >= ?",$exp['used_money']);
			}			
		}		
        $select->order('c.id DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix . 'customer'), array('num_customer' => 'COUNT(*)'));
        if ($exp) {
			if (isset($exp['customer_id'])) {
				$select->where("c.id=?",$exp['customer_id']);
			}
            if (isset($exp['name'])) {
				$select->where("c.name= LIKE '%".$exp['name']."%'");				
			}
            if (isset($exp['tel'])) {
				$select->where("c.tel= LIKE '%".$exp['tel']."%'");				
			}
            if (isset($exp['address'])) {
				$select->where("c.address= LIKE '%".$exp['address']."%'");				
			}
            if (isset($exp['birthday'])) {
				$select->where("c.birthday= ?",$exp['birthday']);				
			}
            if (isset($exp['sex'])) {
				$select->where("c.sex= ?",$exp['sex']);				
			}
            if (isset($exp['first_time'])) {
				$select->where("c.first_time >= ",$exp['first_time']);				
			}
            if (isset($exp['last_time'])) {
				$select->where("c.last_time <= ?",$exp['last_time']);				
			}
            if (isset($exp['amount_time'])) {
				$select->where("c.amount_time >= ?",$exp['amount_time']);				
			}
            if (isset($exp['first_deposit'])) {
				$select->where("c.first_deposit >= ?",$exp['first_deposit']);				
			}
            if (isset($exp['remaining_money'])) {
				$select->where("c.remaining_money >= ?",$exp['remaining_money']);				
			}	
            if (isset($exp['employee_id'])) {
				$select->where("c.employee_id = ?",$exp['employee_id']);				
			}
			if(isset($exp['referrer_id'])){
				$select->where("c.referrer_id = ?",$exp['referrer_id']);
			}
			if(isset($exp['email'])){
				$select->where("c.email = ?",$exp['email']);
			}
			if(isset($exp['used_money'])){
				$select->where("c.used_money >= ?",$exp['used_money']);
			}		
        }
        return $select->query()->fetch()->num_customer;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'customer'))
					->where('c.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Customer_Models_Customer($row);
	}

	public function add($customer) 
	{
			$data = array(
							"id"				=> $customer->id,
							"shop_id"			=> $customer->shop_id,
							"name"				=> $customer->name,
							"tel"				=> $customer->tel,
							"address"			=> $customer->address,
							"birthday"			=> $customer->birthday,
							"sex"				=> $customer->sex,
							"first_time"		=> $customer->first_time,
							"last_time"			=> $customer->last_time,
							"amount_time"		=> $customer->amount_time,
							"images"			=> $customer->images,
							"first_deposit"		=> $customer->first_deposit,
							"remaining_money"	=> $customer->remaining_money,
							"currency"			=> $customer->currency,
							"employee_id"		=> $customer->employee_id,
							"note"				=> $customer->note,
							"referrer_id"		=> $customer->referrer_id,
							"email"				=> $customer->email,
							"used_money"		=> $customer->used_money			
						);			
			if (isset($customer->id) && $customer->id != null) {
				$data['id'] = $customer->id;
				$this->_conn->insert($this->_prefix . 'customer', $data);
				return $customer->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'customer', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'customer');
			}
	}
	
	public function update($customer) 
	{
		$this->_conn->update($this->_prefix . 'customer', 
							array(
								"id"				=> $customer->id,
								"shop_id"			=> $customer->shop_id,
								"name"				=> $customer->name,
								"tel"				=> $customer->tel,
								"address"			=> $customer->address,
								"birthday"			=> $customer->birthday,
								"sex"				=> $customer->sex,
								"first_time"		=> $customer->first_time,
								"last_time"			=> $customer->last_time,
								"amount_time"		=> $customer->amount_time,
								"images"			=> $customer->images,
								"first_deposit"		=> $customer->first_deposit,
								"remaining_money"	=> $customer->remaining_money,
								"currency"			=> $customer->currency,
								"employee_id"		=> $customer->employee_id,
								"note"				=> $customer->note,
								"referrer_id"		=> $customer->referrer_id,
								"email"				=> $customer->email,
								"used_money"		=> $customer->used_money			
							), 
							array(
								'id = ?' => $customer->id,
							));
	}
	
	public function delete($customer) 
	{
		$this->_conn->delete($this->_prefix . 'customer', 
							array(
								'id = ?' => $customer->id,
							));
	}	
}